<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<script>
  /* 参数 */
  // function add(x, y) {
  //   return x + y
  // }

  /*柯里化 : 可数的可控性*/

  function curryingAdd(x) {
    // 多处理一下
    return function (y) {
      // 多处理一下
      return x + y;
    }
  }

  /* 执行柯里化函数 */
  console.log(curryingAdd(10)(20));

  // 函数封装后
  function check(reg, txt) {
    return reg.test(txt)
  }

  console.log(check(/\d+/g, 'test'));       //false
  console.log(check(/[a-z]+/g, 'test'));    //true

  function curryingCheck(reg) {
    return function (txt) {
      return reg.test(txt);
    }
  }

  console.log(curryingCheck(/\d+/g)('test'));

  add(1, 2, 3, 4, 5);

  function add(n1, n2, n3, n4, n5) {
    return n1 + n2 + n3 + n4 + n5;
  }

  function curry(fn) {
    if (fn.length <= 1) return fn;
    // 递归
    const generator = (...args) => {
      // args.length  === fn.length fn()
      if (args.length === fn.length) {
        return fn(...args);
      } else {
        return (...args2) => {
          return generator(...args, ...args2);
        }
      }
    };
    return generator;
  }

  const curriedAdd = curry(add);
  let resFn1 = curriedAdd(1);
  let args2 = 2 * 2;
  // 过程性质的操作
  let res = resFn1(args2)(10)(20)(20);
  console.log(res);
  
  // add(1,2,3)(4)
  // add(1)(2)(3)(4)
  // add(1,2)(3,4)



</script>
</body>
</html>
